package com.zhhtech.zhh.ui.activity.main

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.InviteBean
import com.zhhtech.zhh.databinding.ActivityMyRecommendBinding
import com.zhhtech.zhh.databinding.ItemWithdrawRecordBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.ext.showEmptyCallback
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.helper.loadsir.registerLoadSir

class MyRecommendActivity : BaseActivity<ActivityMyRecommendBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private val mAdapter = MyRecommendAdapter()

    override fun initView() {
        mLoadService = vb.loadview.registerLoadSir(::onReload)
        bindViewClick(vb.buttonSubmit)
        onReload()
    }

    private fun onReload() {
        launchUI({
            val inviteList = api.inviteList().toData() ?: return@launchUI
            vb.tvTodayNum.text = inviteList.todayNum
            vb.tvTotalNum.text = inviteList.totalNum

            mAdapter.datas = inviteList.list
            vb.rvList.layoutManager = LinearLayoutManager(mThis)
            vb.rvList.addItemDecoration(DividerItemDecoration(mThis, DividerItemDecoration.VERTICAL))
            vb.rvList.adapter = mAdapter
        }, ApiObserver(loadService = mLoadService)) {
            if (mAdapter.datas.isEmpty()) {
                mLoadService.showEmptyCallback()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonSubmit -> startActivityExt<MainActivity> {
                putExtra("position", 2)
            }
        }
    }
}

class MyRecommendAdapter : BaseRecyclerAdapter<ItemWithdrawRecordBinding, InviteBean>() {
    override fun onBindViewHolder(vb: ItemWithdrawRecordBinding, bean: InviteBean, position: Int) {
        vb.tvPhone.text = bean.phone
        vb.tvCrateAt.text = bean.crateAt
        vb.tvIncome.text = bean.income
        setViewClick(position, vb.root)
    }
}